﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleProject
{
    /// <summary>
    /// 深拷贝 与浅拷贝
    /// </summary>
    public class Sex
    {
        private string _pSex;
        public string PSex
        {
            get { return _pSex; }
            set { _pSex = value; }
        }
    }

    public class PersonA
    {
        public int aa = 123;
        private Sex obj_Sex = new Sex();  //性别对象

        //性别
        public string PSex
        {
            get { return obj_Sex.PSex; }
            set { obj_Sex.PSex = value; }
        }

        //姓名
        private string _pName;
        public string PName
        {
            get { return _pName; }
            set { _pName = value; }
        }

        /// <summary>
        /// 年龄
        /// </summary>
        private int _age;
        public int Age
        {
            get { return _age; }
            set { _age = value; }
        }

        /// <summary>
        /// 显示方法
        /// </summary>
        public void ShowPersonInfo(string str)
        {
            Console.WriteLine(str);
            Console.WriteLine("---     Name: {0} ; Age: {1}; Sex: {2}; {3}", PName, Age, this.PSex, this.aa);            

        }

        /// <summary>
        /// 浅拷贝
        /// </summary>
        /// <returns></returns>
        public object Clone()
        {
            return this.MemberwiseClone();
        }

        /// <summary>
        /// 深拷贝
        /// </summary>
        /// <returns></returns>
        public object DeepClone()
        {
            PersonA person = new PersonA();
            person.PName = this._pName;
            person.PSex = this.PSex;
            person.Age = this._age;
            return person;
        }
    }

    public class CloneTest
    {
        public void Test()
        {
            PersonA p = new PersonA();            
            p.PName = "jack";
            p.Age = 18;
            p.PSex = "男";
            p.ShowPersonInfo("原对象");

            //浅拷贝
            PersonA personClone = (PersonA)p.Clone();
            
            //深拷贝
            PersonA personDeep = (PersonA)p.DeepClone();

            Console.WriteLine();
            p.PName = "Tom";
            p.Age = 20;
            p.PSex = "女";
            p.ShowPersonInfo("修改原对象");

            //修改后的浅拷贝
            personClone.ShowPersonInfo("浅拷贝 ");

            //修改后的深拷贝
            personDeep.ShowPersonInfo("深拷贝 ");

            PersonA p1 = p;
            p1.ShowPersonInfo("直接拷贝对象");


            Console.WriteLine();
            personClone.PName = "zhaoyan";
            personClone.PSex = "男";
            personClone.Age = 27;
            personClone.ShowPersonInfo("修改浅拷贝对象");

            p.ShowPersonInfo("修改浅拷贝对象后 原对象");
            personDeep.ShowPersonInfo("修改浅拷贝对象后 深拷贝的对象");

            
        }
    }
}
